package tool;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import org.apache.poi.hwpf.extractor.WordExtractor;
import org.apache.poi.hwpf.model.io.HWPFOutputStream;

public class Word {
	/**
	 * 读取纯文本的word文件
	 */
	public String readWord(String doc) throws Exception {
		String context = null;
		WordExtractor extractor = null; // 纯文本的遍历器
		try {
			FileInputStream in = new FileInputStream(new File(doc));

			extractor = new WordExtractor(in);
			context = extractor.getText();
		} catch (Exception e) {
			e.printStackTrace();
		}

		return context;
	}

	/**
	 * 对word文档做写操作
	 * 
	 */
	public static boolean writeWord(String path, String content) throws Exception {
		boolean w = false;
		try {
			byte b[] = content.getBytes();
			FileOutputStream fs = new FileOutputStream(path);
			HWPFOutputStream hos = new HWPFOutputStream();

			hos.write(b, 0, b.length);
			hos.writeTo(fs);
			hos.close();
			w = true;
		} catch (Exception e) {
			e.printStackTrace();
		}

		return w;

	}

	public static void main(String[] args) {
		Word rw = new Word();

		try {
//			String text = rw.readWord("D:\\workspace\\MyUntil\\t.doc");
			String text="123";
			rw.writeWord("D:\\workspace\\MyUntil\\d.doc", text);
		} catch (Exception e) {
			e.printStackTrace();

		}

	}

}
